<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveSupport::Concern</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActiveSupport::Concern 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../files/activesupport/lib/active_support/concern_rb.html">activesupport/lib/active_support/concern.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<p>A typical module looks like this:</p>

<pre class="ruby"><span class="ruby-keyword">module</span> <span class="ruby-constant">M</span>
  <span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">included</span>(<span class="ruby-identifier">base</span>)
    <span class="ruby-identifier">base</span>.<span class="ruby-identifier">extend</span> <span class="ruby-constant">ClassMethods</span>
    <span class="ruby-identifier">base</span>.<span class="ruby-identifier">class_eval</span> <span class="ruby-keyword">do</span>
      <span class="ruby-identifier">scope</span> :<span class="ruby-identifier">disabled</span>, <span class="ruby-operator">-</span><span class="ruby-operator">&gt;</span> { <span class="ruby-identifier">where</span>(<span class="ruby-identifier">disabled</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>) }
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">module</span> <span class="ruby-constant">ClassMethods</span>
    <span class="ruby-operator">...</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>By using <code>ActiveSupport::Concern</code> the above module could instead
be written as:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'active_support/concern'</span>

<span class="ruby-keyword">module</span> <span class="ruby-constant">M</span>
  <span class="ruby-identifier">extend</span> <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Concern</span>

  <span class="ruby-identifier">included</span> <span class="ruby-keyword">do</span>
    <span class="ruby-identifier">scope</span> :<span class="ruby-identifier">disabled</span>, <span class="ruby-operator">-</span><span class="ruby-operator">&gt;</span> { <span class="ruby-identifier">where</span>(<span class="ruby-identifier">disabled</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>) }
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">module</span> <span class="ruby-constant">ClassMethods</span>
    <span class="ruby-operator">...</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Moreover, it gracefully handles module dependencies. Given a
<code>Foo</code> module and a <code>Bar</code> module which depends on the
former, we would typically write the following:</p>

<pre class="ruby"><span class="ruby-keyword">module</span> <span class="ruby-constant">Foo</span>
  <span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">included</span>(<span class="ruby-identifier">base</span>)
    <span class="ruby-identifier">base</span>.<span class="ruby-identifier">class_eval</span> <span class="ruby-keyword">do</span>
      <span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">method_injected_by_foo</span>
        <span class="ruby-operator">...</span>
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">module</span> <span class="ruby-constant">Bar</span>
  <span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">included</span>(<span class="ruby-identifier">base</span>)
    <span class="ruby-identifier">base</span>.<span class="ruby-identifier">method_injected_by_foo</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">Host</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">Foo</span> <span class="ruby-comment"># We need to include this dependency for Bar</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">Bar</span> <span class="ruby-comment"># Bar is the module that Host really needs</span>
<span class="ruby-keyword">end</span>
</pre>

<p>But why should <code>Host</code> care about <code>Bar</code>‘s
dependencies, namely <code>Foo</code>? We could try to hide these from
<code>Host</code> directly including <code>Foo</code> in <code>Bar</code>:</p>

<pre class="ruby"><span class="ruby-keyword">module</span> <span class="ruby-constant">Bar</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">Foo</span>
  <span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">included</span>(<span class="ruby-identifier">base</span>)
    <span class="ruby-identifier">base</span>.<span class="ruby-identifier">method_injected_by_foo</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">Host</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">Bar</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Unfortunately this won’t work, since when <code>Foo</code> is included, its
<code>base</code> is the <code>Bar</code> module, not the <code>Host</code>
class. With <code>ActiveSupport::Concern</code>, module dependencies are
properly resolved:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'active_support/concern'</span>

<span class="ruby-keyword">module</span> <span class="ruby-constant">Foo</span>
  <span class="ruby-identifier">extend</span> <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Concern</span>
  <span class="ruby-identifier">included</span> <span class="ruby-keyword">do</span>
    <span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">method_injected_by_foo</span>
      <span class="ruby-operator">...</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">module</span> <span class="ruby-constant">Bar</span>
  <span class="ruby-identifier">extend</span> <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Concern</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">Foo</span>

  <span class="ruby-identifier">included</span> <span class="ruby-keyword">do</span>
    <span class="ruby-keyword">self</span>.<span class="ruby-identifier">method_injected_by_foo</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">Host</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">Bar</span> <span class="ruby-comment"># works, Bar takes care now of its dependencies</span>
<span class="ruby-keyword">end</span>
</pre>

    </div>
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>A</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Concern.html#method-i-append_features">append_features</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>I</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Concern.html#method-i-included">included</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-append_features">
            
              <b>append_features</b>(base)
            
            <a href="Concern.html#method-i-append_features" name="method-i-append_features" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-append_features_source')" id="l_method-i-append_features_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/2ff888181d8d3425864f58141881e36b5f12e8c3/activesupport/lib/active_support/concern.rb#L105" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-append_features_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/concern.rb, line 105</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">append_features</span>(<span class="ruby-identifier">base</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">base</span>.<span class="ruby-identifier">instance_variable_defined?</span>(<span class="ruby-string">&quot;@_dependencies&quot;</span>)
    <span class="ruby-identifier">base</span>.<span class="ruby-identifier">instance_variable_get</span>(<span class="ruby-string">&quot;@_dependencies&quot;</span>) <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-keyword">self</span>
    <span class="ruby-keyword">return</span> <span class="ruby-keyword">false</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-keyword">return</span> <span class="ruby-keyword">false</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">base</span> <span class="ruby-operator">&lt;</span> <span class="ruby-keyword">self</span>
    <span class="ruby-ivar">@_dependencies</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">dep</span><span class="ruby-operator">|</span> <span class="ruby-identifier">base</span>.<span class="ruby-identifier">send</span>(<span class="ruby-value">:include</span>, <span class="ruby-identifier">dep</span>) }
    <span class="ruby-keyword">super</span>
    <span class="ruby-identifier">base</span>.<span class="ruby-identifier">extend</span> <span class="ruby-identifier">const_get</span>(<span class="ruby-string">&quot;ClassMethods&quot;</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">const_defined?</span>(<span class="ruby-string">&quot;ClassMethods&quot;</span>)
    <span class="ruby-identifier">base</span>.<span class="ruby-identifier">class_eval</span>(&amp;<span class="ruby-ivar">@_included_block</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">instance_variable_defined?</span>(<span class="ruby-string">&quot;@_included_block&quot;</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-included">
            
              <b>included</b>(base = nil, &amp;block)
            
            <a href="Concern.html#method-i-included" name="method-i-included" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-included_source')" id="l_method-i-included_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/2ff888181d8d3425864f58141881e36b5f12e8c3/activesupport/lib/active_support/concern.rb#L118" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-included_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/concern.rb, line 118</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">included</span>(<span class="ruby-identifier">base</span> = <span class="ruby-keyword">nil</span>, &amp;<span class="ruby-identifier">block</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">base</span>.<span class="ruby-identifier">nil?</span>
    <span class="ruby-ivar">@_included_block</span> = <span class="ruby-identifier">block</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-keyword">super</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    